package com.toe.user.webapi.service.impl;

import com.toe.pojo.dto.user.UserRoleAddDTO;
import com.toe.pojo.dto.user.UserRolePermissionAddDTO;
import com.toe.pojo.entity.user.UserRole;
import com.toe.pojo.entity.user.UserRolePermission;
import com.toe.pojo.vo.user.UserRoleVO;
import com.toe.user.service.UserRolePermissionService;
import com.toe.user.webapi.mapper.UserAdminRoleMapper;
import com.toe.user.webapi.mapper.UserBusinessRoleMapper;
import com.toe.user.webapi.mapper.UserRoleMapper;
import com.toe.user.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.user.webapi.mapper.UserRolePermissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserRolePermissionService userRolePermissionService;

    @Autowired
    private UserRolePermissionMapper userRolePermissionMapper;

    @Autowired
    private UserAdminRoleMapper userAdminRoleMapper;

    @Autowired
    private UserBusinessRoleMapper userBusinessRoleMapper;

    @Override
    public void insert(UserRoleAddDTO userRoleAddDTO) {
        UserRole userRole = new UserRole();
        BeanUtils.copyProperties(userRoleAddDTO,userRole);
        LocalDateTime date = LocalDateTime.now();
        userRole.setCreateTime(date);
        userRole.setUpdateTime(date);
        userRoleMapper.insert(userRole);
    }

    @Override
    public void delete(long id) {
        userRoleMapper.deleteById(id);
        userRolePermissionMapper.deleteByRoleId(id);
        //还需添加删除admin与role的映射
        userAdminRoleMapper.deleteByRoleId(id);
        userBusinessRoleMapper.deleteByRoleId(id);
    }

    @Override
    public List<UserRoleVO> selectAll() {
        List<UserRoleVO> userRoleVOS = userRoleMapper.selectAll();
        return userRoleVOS;
    }

    @Override
    public UserRoleVO selectOne(long roleId) {
        UserRoleVO userRoleVO = userRoleMapper.selectOne(roleId);
        return userRoleVO;
    }

    @Override
    public void addRolePermission(long roleId, long permissionId) {
        UserRolePermissionAddDTO addDTO = new UserRolePermissionAddDTO();
        addDTO.setRoleId(roleId);
        addDTO.setPermissionId(permissionId);
        userRolePermissionService.insert(addDTO);
    }
}
